<template>
  <header class="header">
    <div class="logo">
      <router-link to="/">
        <h1>在线教育系统</h1>
      </router-link>
    </div>
    <div class="nav-links">
      <router-link to="/courses">课程中心</router-link>
      <router-link to="/homework">作业管理</router-link>
      <router-link to="/chat">在线交流</router-link>
    </div>
    <div class="user-section">
      <NotificationCenter />
      <div class="user-info" v-if="user">
        <img :src="user.avatar || '/default-avatar.png'" alt="用户头像" class="avatar">
        <div class="dropdown">
          <span>{{ user.name }}</span>
          <div class="dropdown-content">
            <router-link to="/profile">个人设置</router-link>
            <a @click="handleLogout">退出登录</a>
          </div>
        </div>
      </div>
      <div v-else class="auth-links">
        <router-link to="/login">登录</router-link>
        <router-link to="/register">注册</router-link>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../../stores/auth.store'
import NotificationCenter from '../notification/NotificationCenter.vue'

const router = useRouter()
const authStore = useAuthStore()
const user = computed(() => authStore.user)

const handleLogout = async () => {
  await authStore.logout()
  router.push('/login')
}
</script>

<style scoped lang="scss">
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

  .logo {
    h1 {
      font-size: 1.5rem;
      color: #333;
      margin: 0;
    }
  }

  .nav-links {
    display: flex;
    gap: 20px;

    a {
      color: #666;
      text-decoration: none;
      font-size: 1rem;

      &:hover {
        color: #1890ff;
      }

      &.router-link-active {
        color: #1890ff;
      }
    }
  }

  .user-section {
    display: flex;
    align-items: center;
    gap: 20px;

    .user-info {
      display: flex;
      align-items: center;
      gap: 10px;
      cursor: pointer;
      position: relative;

      .avatar {
        width: 32px;
        height: 32px;
        border-radius: 50%;
      }

      .dropdown {
        &:hover .dropdown-content {
          display: block;
        }

        .dropdown-content {
          display: none;
          position: absolute;
          right: 0;
          top: 100%;
          background-color: #fff;
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
          border-radius: 4px;
          padding: 4px 0;

          a {
            display: block;
            padding: 8px 16px;
            color: #666;
            text-decoration: none;

            &:hover {
              background-color: #f5f5f5;
            }
          }
        }
      }
    }

    .auth-links {
      display: flex;
      gap: 10px;

      a {
        color: #666;
        text-decoration: none;

        &:hover {
          color: #1890ff;
        }
      }
    }
  }
}
</style>